package com.futures.questionbank.main;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.futures.questionbank.R;
import com.futures.questionbank.Utils.UItils;
import com.futures.questionbank.base.BaseActivity;
import com.futures.questionbank.db.table.User;
import com.futures.questionbank.view.CustomLoadingDialog;
import com.raizlabs.android.dbflow.sql.language.Select;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author : huang
 * date   : 2020/6/15 0015 16:33
 * version: 1.0
 */
public class RegisterActivity extends BaseActivity {
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.et_password_two)
    EditText etPasswordTwo;
    @BindView(R.id.tv_user_info)
    TextView tvUserInfo;
    @BindView(R.id.sb_register)
    SuperButton sbRegister;
    @BindView(R.id.tv_charge)
    TextView tvCharge;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.register_activity);
        setTitle("注册");
    }

    @Override
    protected void loadData() {

    }



    @OnClick({R.id.sb_register, R.id.tv_charge,R.id.tv_user_info,R.id.tv_zhengce})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.sb_register:
                if (!UItils.isPhoneNumber(etPhone.getText().toString().trim())) {
                    UItils.showToastSafe("请输入正确的手机号码");
                    return;
                }

                if (TextUtils.isEmpty(etPassword.getText().toString().trim())) {
                    UItils.showToastSafe(etPassword.getHint().toString());
                    return;
                }
                if (TextUtils.isEmpty(etPasswordTwo.getText().toString().trim())) {
                    UItils.showToastSafe(etPasswordTwo.getHint().toString());
                    return;
                }
                CustomLoadingDialog.show(RegisterActivity.this);
                //根据 name 单个查询
                List<User> user = new Select()
                        .from(User.class)
                        .queryList();
                List<String> list = new ArrayList<>();
                for (User user1 : user) {
                    list.add(user1.getPhone());
                }
                if (null != user){
                    if (!list.contains(etPhone.getText().toString().trim())){
                        User userModel= new User();
                        userModel.setPhone(etPhone.getText().toString().trim());
                        userModel.setPassword(etPassword.getText().toString().trim());
                        userModel.save();
                        final Timer t = new Timer();
                        t.schedule(new TimerTask() {
                            @Override
                            public void run() {
                                UItils.showToastSafe("注册成功");
                                UItils.startActivity(MainActivity2.class);
                                finish();
                                CustomLoadingDialog.dismiss(RegisterActivity.this);
                                t.cancel();
                            }
                        }, 2000);
                    }else {
                        UItils.showToastSafe("帐号已注册");
                        CustomLoadingDialog.dismiss(RegisterActivity.this);
                    }
            }
                break;
            case R.id.tv_charge:
                finish();
                break;
            case R.id.tv_user_info:
                Intent meg = new Intent(this, AboutAgreementActivity.class);
                meg.putExtra("type",0);
                startActivity(meg);
                break;
            case R.id.tv_zhengce:
                Intent intent = new Intent(this,AboutAgreementActivity.class);
                intent.putExtra("type",1);
                startActivity(intent);
                break;
                default:
        }
    }
}
